﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using log4net;
using StockFinder.Model;
using StockFinder.StockLists.SymbolListEnumerators;

namespace StockFinder.StockLists
{
    public class YahooFinanceOfflineDirectoryBrowserStockList : YahooFinanceOfflinePriceLoaderSymbolListEnumerator
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(YahooFinanceOfflineDirectoryBrowserStockList));

        public string DirectoryLocation { get; set; }

        protected override Dictionary<Symbol, string> GetSymbols()
        {
            if (!Directory.Exists(DirectoryLocation))
            {
                Log.WarnFormat("Directory at {0} doesnt exist", DirectoryLocation);
                return null;
            }

            var filesInDirectory = Directory.EnumerateFiles(DirectoryLocation);

            return filesInDirectory.ToDictionary(file => new Symbol {Name = GetSymbolNameFromFileName(file)});
        }

        private static string GetSymbolNameFromFileName(string filename)
        {
            var fileInfo = new FileInfo(filename);
            return fileInfo.Name.Replace(fileInfo.Extension, string.Empty);
        }
    }
}
